package com.dks.repository.impl.teacher;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.aliyuncs.utils.StringUtils;
import com.dks.dto.StudentClassDto;
import com.dks.dto.TeacherCourseDto;
import com.dks.dto.TeacherInfoDto;
import com.dks.mapper.TeacherMapper;
import com.dks.models.Teacher;
import com.dks.repository.ITeacherRepository;
import com.dks.result.Result;
import com.dks.result.ResultInfo;
import com.dks.result.ResultModel;
import com.dks.util.PageInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class TeacherRepository implements ITeacherRepository {

    private TeacherMapper teacherMapper;

    @Autowired
    public TeacherRepository(final TeacherMapper teacherMapperIn) {
        this.teacherMapper = teacherMapperIn;
    }

    @Override
    public Integer insert(Teacher teacher) {
        return teacherMapper.insert(teacher);
    }

    @Override
    public Integer update(Teacher teacher) {
        return teacherMapper.update(teacher);
    }

    @Override
    public Integer delete(Integer id) {
        return teacherMapper.delete(id);
    }

    @Override
    public Teacher select(Integer id) {
        return teacherMapper.select(id);
    }

    @Override
    public Integer cntByMap(Map<String, Object> map) {
        return teacherMapper.cntByMap(map);
    }

    @Override
    public List<Integer> getByMap(Map<String, Object> map) {
        return teacherMapper.getByMap(map);
    }

    @Override
    public List<Teacher> getByMapList(Map<String, Object> map) {
        return teacherMapper.getByMapList(map);
    }

    @Override
    public Integer updateByCode(Teacher teacher) {
        return teacherMapper.updateByCode(teacher);
    }

    @Override
    public Teacher selectByBfCode(String bfCode) {
        return teacherMapper.selectByBfCode(bfCode);
    }


    /**
     * @Author: Ah Nan
     * @Description: 获取课程授课教师列表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: courseId 家长id
     * @Param: pageNum 页码
     * @Param: pageSize 条数
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getCourseTeacherList(String courseStage, String name, Integer pageNum, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("courseStage", courseStage);
        map.put("name", name);
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<Teacher> teacherList = teacherMapper.getCourseTeacherList(map);
        PageInfo<Teacher> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), teacherList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
    }

    /**
     * @Author: Ah Nan
     * @Description: 获取教师详情
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: teacherId 教师id
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getTeacherInfo(Integer teacherId,Integer userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("teacherId", teacherId);
        map.put("userId", userId);
        TeacherInfoDto teacher = teacherMapper.selectTeacherInfo(map);
        return Result.makeResult(ResultInfo.SUCCESS, teacher);
    }

    /**
     * @Author: Ah Nan
     * @Description: 教务端 获取教师的课程列表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: teacherId 老师id
     * @Param: pageNum 页码
     * @Param: pageSize 条数
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> tcGetTeacherCourseList(String beginTime, String endTime, String courseCategory, Integer teacherId, Integer pageNum, Integer pageSize) {

        Map<String, Object> map = new HashMap<>();
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("teacherId", teacherId);
        map.put("courseCategory", courseCategory);
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<TeacherCourseDto> teacherList = teacherMapper.getTeacherCourseDtoList(map);
        PageInfo<TeacherCourseDto> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), teacherList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
    }

    /**
     * @Author: Ah Nan
     * @Description: 【教务】获取系统的全部课表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: teacherId 老师id
     * @Param: pageNum 页码
     * @Param: pageSize 条数
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> tcGetAllTeacherCourseList(Long teacherId, String beginTime, String endTime, String courseCategory, String studentBirthDay, Integer pageNum, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("courseCategory", courseCategory);
        map.put("teacherId", teacherId);
        if (!StringUtils.isEmpty(studentBirthDay)) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date date = simpleDateFormat.parse(studentBirthDay);
                long birthMonth = ((new Date()).getTime() - date.getTime())/1000/60/60/24/30;
                map.put("babyBirthMonth", birthMonth);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<TeacherCourseDto> teacherList = teacherMapper.getTeacherCourseDtoList(map);
        PageInfo<TeacherCourseDto> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), teacherList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
    }

    /**
     * @Author: Ah Nan
     * @Description: 【教务】获取上课人员列表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: teacherId 老师id
     * @Param: pageNum 页码
     * @Param: pageSize 条数
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> tcGetStudentCourseList(String appointmentType, Integer classId, Integer pageNum, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("appointmentType", appointmentType);
        map.put("classId", classId);
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<StudentClassDto> teacherList = teacherMapper.getStudentClassDtoList(map);
        PageInfo<StudentClassDto> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), teacherList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
    }

    /**
     * @Author: Ah Nan
     * @Description: 【教务】获取全部老师列表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: teacherId 老师id
     * @Param: pageNum 页码
     * @Param: pageSize 条数
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getAllTeacherList(String teacherName, Integer pageNum, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", teacherName);
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<Teacher> teacherList = teacherMapper.getCourseTeacherList(map);
        PageInfo<Teacher> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), teacherList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
    }

    /**
     * @Author: Ah Nan
     * @Description: 获取课程分类对应的老师列表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: teacherId 老师id
     * @Param: pageNum 页码
     * @Param: pageSize 条数
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getCourseCategoryTeacherList(Integer courseCategoryId, Integer pageNum, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("courseCategoryId", courseCategoryId);
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<Teacher> teacherList = teacherMapper.getCourseCategoryTeacherList(map);
        PageInfo<Teacher> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), teacherList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
    }

}
